package top.yangbuyi.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import top.yangbuyi.constants.Constants;
import top.yangbuyi.domain.CheckItem;
import top.yangbuyi.dto.CheckItemDto;
import top.yangbuyi.mapper.CheckItemMapper;
import top.yangbuyi.service.impl.CheckItemService;
import top.yangbuyi.vo.DataGridView;

/**
 * ClassName: Loginfo
 * Description: 杨不易网站 :www.yangbuyi.top
 * date: 2020/9/11
 *
 * @author TeouBle
 * @author yangbuyi
 * @since JDK 1.8
 **/

@Service
public class CheckItemServiceImpl implements CheckItemService {
	  @Autowired
	  private CheckItemMapper checkItemMapper;

	  @Override
	  public DataGridView listCheckItemPage(CheckItemDto checkItemDto) {
			Page<CheckItem> page = new Page<>(checkItemDto.getPageNum(), checkItemDto.getPageSize());
			QueryWrapper<CheckItem> qw = new QueryWrapper<>();
			qw.like(StringUtils.isNotBlank(checkItemDto.getCheckItemName()), CheckItem.COL_CHECK_ITEM_NAME, checkItemDto.getCheckItemName());
			qw.like(StringUtils.isNotBlank(checkItemDto.getKeywords()), CheckItem.COL_KEYWORDS, checkItemDto.getKeywords());
			qw.eq(StringUtils.isNotBlank(checkItemDto.getTypeId()), CheckItem.COL_TYPE_ID, checkItemDto.getTypeId());
			qw.eq(StringUtils.isNotBlank(checkItemDto.getStatus()), CheckItem.COL_STATUS, checkItemDto.getStatus());
			this.checkItemMapper.selectPage(page, qw);
			return new DataGridView(page.getTotal(), page.getRecords());
	  }

	  /**
	   * 查询所有可用的检查项目
	   *
	   * @return
	   */
	  @Override
	  public List<CheckItem> selectAllCheckItem() {
			QueryWrapper<CheckItem> qw = new QueryWrapper<>();
			qw.eq(CheckItem.COL_STATUS, Constants.STATUS_TRUE);
			this.checkItemMapper.selectList(qw);
			return this.checkItemMapper.selectList(qw);
	  }

	  @Override
	  public CheckItem getOne(Long checkItemId) {
			return this.checkItemMapper.selectById(checkItemId);
	  }

	  @Override
	  public int addCheckItem(CheckItemDto checkItemDto) {
			CheckItem checkItem = new CheckItem();
			BeanUtil.copyProperties(checkItemDto, checkItem);
			checkItem.setCreateTime(DateUtil.date());
			checkItem.setCreateBy(checkItemDto.getSimpleUser().getUserName());
			return this.checkItemMapper.insert(checkItem);
	  }

	  @Override
	  public int updateCheckItem(CheckItemDto checkItemDto) {
			CheckItem checkItem = new CheckItem();
			BeanUtil.copyProperties(checkItemDto, checkItem);
			checkItem.setUpdateBy(checkItemDto.getSimpleUser().getUserName());
			return this.checkItemMapper.updateById(checkItem);
	  }

	  @Override
	  public int deleteCheckItemByIds(Long[] checkItemIds) {
			List<Long> ids = Arrays.asList(checkItemIds);
			if (ids.size() > 0) {
				  return this.checkItemMapper.deleteBatchIds(ids);
			}
			return 0;
	  }


}
